﻿using System;
using System.Text.Json.Serialization;

namespace TowerCraneMonitorClient.NetClient.RunData
{
    /// <summary>
    /// RunData 实体
    /// </summary>
    public class Entity : NetClient.Entity
    {
        /// <summary>
        /// 采集时间。Json（unix时间戳）
        /// </summary>
        [JsonPropertyName("time")]
        public long TimeStamp => Time.ToUnixTimeStamp().ToLong();

        /// <summary>
        /// 采集时间。数据库时间
        /// </summary>
        [JsonIgnore]
        public DateTime Time { get; set; }

        /// <summary>
        /// 当前转角
        /// </summary>
        public float Angle { get; set; }

        /// <summary>
        /// 变幅
        /// </summary>
        public float Extent { get; set; }

        /// <summary>
        /// 高度
        /// </summary>
        public float Height { get; set; }

        /// <summary>
        /// 当前载重
        /// </summary>
        public float Weight { get; set; }

        /// <summary>
        /// 当前安全载重
        /// </summary>
        public float SafeWeight { get; set; }

        /// <summary>
        /// 安全载重百分比.范围:0-100
        /// </summary>
        public float WeightPer
        {
            get => weightPer;
            set
            {
                if (value < 0 || value > 100)
                    throw new PercentageArgumentOutOfRangeException(
                        nameof(WeightPer));
                weightPer = value;
            }
        }
        float weightPer;

        /// <summary>
        /// 当前力矩
        /// </summary>
        public float Force { get; set; }

        /// <summary>
        /// 当前力矩百分比.范围:0-100
        /// </summary>
        public float ForcePer
        {
            get => forcePer;
            set
            {
                if (value < 0 || value > 100)
                    throw new PercentageArgumentOutOfRangeException(
                        nameof(ForcePer));
                forcePer = value;
            }
        }
        float forcePer;

        /// <summary>
        /// 风速
        /// </summary>
        public float Wind { get; set; }

        /// <summary>
        /// 当前驾驶员身份证号
        /// </summary>
        public string DriverId => DriverInfo.IDCard;

        /// <summary>
        /// 当前驾驶员姓名.如未采集到可传递空串
        /// </summary>
        public string DriverName => DriverInfo.Name;

        /// <summary>
        /// 电源状态 0正常供电, 1备用电源供电
        /// </summary>
        public int Power
        {
            get => power;
            set { power = value == 0 ? 0 : 1; }
        }
        int power;

        /// <summary>
        /// 当前状态
        /// </summary>
        public Status Status { get; set; }

        /// <summary>
        /// 塔身倾斜角度°X向与塔机坐标X轴向一致，如安装方向不一致请转换
        /// </summary>
        public float ObliqueAngleX { get; set; }

        /// <summary>
        /// 塔身倾斜角度°Y向与塔机坐标Y轴向一致，如安装方向不一致请转换
        /// </summary>
        public float ObliqueAngleY { get; set; }

        /// <summary>
        /// 限位码
        /// </summary>
        public int LimitAlarmCode { get; set; }


        /// <summary>
        /// 导航属性：驾驶员信息
        /// </summary>
        [JsonIgnore]
        public DriverInfo.Entity DriverInfo { get; set; }

        /// <summary>
        /// 基于该对象生成 RunData.Req 报文
        /// </summary>
        /// <param name="errorMessage">失败时返回错误信息，成功时返回 null</param>
        /// <returns>成功时返回 RunData.Req 对象，失败时返回 null</returns>
        public new Req ToReq() => new Req(this);
    }
}
